package pers.dawnyang.modular.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import pers.dawnyang.common.domain.entity.SysDictType;
import pers.dawnyang.common.domain.entity.SysDictValue;
import pers.dawnyang.config.cache.DictUtils;
import pers.dawnyang.modular.system.domain.response.common.DictValue;
import pers.dawnyang.modular.system.mapper.SysDictTypeMapper;
import pers.dawnyang.modular.system.mapper.SysDictValueMapper;
import pers.dawnyang.modular.system.service.DictTypeService;

@Service
public class DictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements DictTypeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private SysDictValueMapper sysDictValueMapper;

    @Override
    public void initDictCache() {
        DictUtils.clearDictCache();
        LambdaQueryWrapper<SysDictType> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(SysDictType::getStatus, 1);
        List<SysDictType> dictTypeList = sysDictTypeMapper.selectList(lqw1);
        for (SysDictType sysDictType : dictTypeList) {
            LambdaQueryWrapper<SysDictValue> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(SysDictValue::getTypeCode, sysDictType.getTypeCode());
            lqw2.eq(SysDictValue::getStatus, 1);
            List<SysDictValue> dictValueList = sysDictValueMapper.selectList(lqw2);
            List<DictValue> valueList = new ArrayList<>();
            for (SysDictValue sysDictValue : dictValueList) {
                DictValue dictValue = new DictValue();
                BeanUtils.copyProperties(sysDictValue, dictValue);
                valueList.add(dictValue);
            }
            // System.out.println(sysDictType.getTypeCode());
            // System.out.println(valueList);
            DictUtils.setDictValueList(sysDictType.getTypeCode(), valueList);
        }

    }

}
